// Demo Capture
// Toutes les secondes, on lit le compteur de TimerCount0 et/ou TimerCount1 et/ou TimerCount2 pour mesurer la frequence
// => permet de mesurer 3 frequences, si besoin d'une seule mesure commenter/effacer les lignes du compteur
// non-utilisees, si besoin de 4 compteurs, uitliser RIT comme Timer pour la generation de la duree fixe
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
#include <cr_section_macros.h>
#include <NXP/crp.h>
// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
#include <stdio.h>

volatile int freq0, freq1, freq2;
void config_pins(void);
void config_capture_counter0(void);   // utilise TimerCounter0 en mode Compteur
void config_capture_counter1(void);   // utilise TimerCounter1 en mode Compteur
void config_capture_counter2(void);   // utilise TimerCounter2 en mode Compteur
void config_fixed_delay_timer3(void); // utilise TimerCounter3 en mode Timer
void TIMER3_IRQHandler(void);

int main(void)
{
	int i=0;
	printf("Capture Demo - Compteur Frequence.\n");

	config_pins();
	config_capture_counter0();
	config_capture_counter1();
	config_capture_counter2();
	config_fixed_delay_timer3();

	while(1)
	{
	  for(i=0; i < 10000000; i++);
		printf("FREQ0 = %d [Hz] - FREQ1 = %d [Hz] - FREQ2 = %d [Hz] \n", freq0, freq1, freq2);
		i++ ;
	}
	return 0 ;
}
void config_pins(void)
{
	// Fonction 11 pour capture sur CAPn.x
	// TimerCounter0
	LPC_PINCON->PINSEL3 |= ((1<<21)|(1<<20)); // P1.26 (PAD9) = CAP0.0 bit 21 & 20 on
	//LPC_PINCON->PINSEL3 |= ((1<<23)|(1<<22)); // P1.27 (PAD10) = CAP0.1 bit 23 & 22 on

	// TimerCounter1
	LPC_PINCON->PINSEL3 |= ((1<<5)|(1<<4));  // P1.18 (PAD1) = CAP1.0 bit 5 & 4 on
	//LPC_PINCON->PINSEL3 |= ((1<<7)|(1<<6));  // P1.19 (PAD2) = CAP1.1 bit 7 & 6 on

	// TimerCounter2
	LPC_PINCON->PINSEL0 |= ((1<<9)|(1<<8));  // P0.4 = CAP2.0 bit 9 & 8 on
	//LPC_PINCON->PINSEL0 |= ((1<<11)|(1<<10));  // P0.5 = CAP2.1 bit 11 & 10 on

	// TimerCounter3 ! Utilise pour la generation de la duree fixe, si besoin, utiliser le RIT
	//LPC_PINCON->PINSEL0 |= ((1<<15)|(1<<14));  // P0.23 = CAP3.0 bit 15 & 14 on
	//LPC_PINCON->PINSEL0 |= ((1<<17)|(1<<16));  // P0.24 = CAP3.1 bit 17 & 16 on
}
// TIMER0 -> utilise comme compteur sur PAD9 (P1.26) ou PAD10 (P1.27)
void config_capture_counter0(void)
{
	LPC_SC->PCONP |= (1<<1);   // TimerCounter0 On
	LPC_TIM0->CTCR = 1;        // Count Control Register: 1=Rising Edges, 2=Falling Edges, 3=Both
	//LPC_TIM1->CTCR |= (1<<2);  // P1.27 = CAP0.1 (PAD10, TimerCounter0)
	LPC_TIM0->CTCR |= (0<<2);  //  P1.26 = CAP0.0 (PAD9, TimerCounter0)
	LPC_TIM0->CCR = 0;         // Capture Control Off en mode Counter
	LPC_TIM0->TCR = (1<<1);    // Reset Compteur
	LPC_TIM0->TCR = 1;   	   // Compteur On
}
// TIMER1 -> utilise comme compteur sur PAD1 (P1.18) ou PAD2 (P1.19)
void config_capture_counter1(void)
{
	LPC_SC->PCONP |= (1<<2);   // TimerCounter1 On
	LPC_TIM1->CTCR = 1;        // Count Control Register: 1=Rising Edges, 2=Falling Edges, 3=Both
	//LPC_TIM1->CTCR |= (1<<2);  // P1.19 = CAP1.1 (PAD2, TimerCounter1)
	LPC_TIM1->CTCR |= (0<<2);  // P1.18 = CAP1.0 (PAD1, TimerCounter1)
	LPC_TIM1->CCR = 0;         // Capture Control Off en mode Counter
	LPC_TIM1->TCR = (1<<1);    // Reset Compteur
	LPC_TIM1->TCR = 1;   	   // Compteur On
}
// TIMER2 -> utilise comme compteur sur (P0.4) ou (P0.5)
void config_capture_counter2(void)
{
	LPC_SC->PCONP |= (1<<22);   // TimerCounter2 On
	LPC_TIM2->CTCR = 1;        // Count Control Register: 1=Rising Edges, 2=Falling Edges, 3=Both
	//LPC_TIM1->CTCR |= (1<<2);  // P0.5 = CAP2.1, TimerCounter2
	LPC_TIM2->CTCR |= (0<<2);  // P0.4 = CAP2.0 TimerCounter2
	LPC_TIM2->CCR = 0;         // Capture Control Off en mode Counter
	LPC_TIM2->TCR = (1<<1);    // Reset Compteur
	LPC_TIM2->TCR = 1;   	   // Compteur On
}
// TIMER3
void config_fixed_delay_timer3(void)
{
	LPC_SC->PCONP |= (1<<23);    // Timer3 On
	LPC_SC->PCLKSEL1 |= (1<<14); // Clock Timer3 = CCLK
	LPC_TIM3->PR = 0;			 // Prescaler
	LPC_TIM3->MR0 = 99999999;    // Si CCLK = 100 MHz -> Int. toutes les secondes
	LPC_TIM3->MCR = 3;	         // Interrupt and Reset on MR0
	NVIC_EnableIRQ(TIMER3_IRQn); // Enable timer interrupt
	LPC_TIM3->TCR = 2;     // Reset Timer0
	LPC_TIM3->TCR = 1;     // Start timer
}
void TIMER3_IRQHandler(void)
{
	if ( (LPC_TIM3->IR & (1<<0)) == (1<<0) )
	{
	 LPC_TIM3->IR |= (1<<0); // RAZ Int.
	  // Counter0
	   freq0 = LPC_TIM0->TC; // Read the counter value
	   LPC_TIM0->TCR |= (1<<1); // Reset the counter
	   LPC_TIM0->TCR = 1; // ReStart
	  // Counter1
	   freq1 = LPC_TIM1->TC; // Read the counter value
	   LPC_TIM1->TCR |= (1<<1); // Reset the counter
	   LPC_TIM1->TCR = 1; // ReStart
	   // Counter2
	   freq2 = LPC_TIM2->TC; // Read the counter value
	   LPC_TIM2->TCR |= (1<<1); // Reset the counter
	   LPC_TIM2->TCR = 1; // ReStart
	 //printf("1 sec\n");
	}
}
